// <copyright file="SafeEnumerableDecorator.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The safe enumerable decorator.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Collections.Concurrent
{
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;

    using SubhadraSolutions.Sharp.Utils.Validation;

    /// <summary>
    /// The safe enumerable decorator.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public sealed class SafeEnumerableDecorator<T> : IEnumerable<T>
    {
        #region Fields

        /// <summary>
        /// The adaptedObject.
        /// </summary>
        private readonly IEnumerable<T> adaptedObject;

        /// <summary>
        /// The reader writer lock
        /// </summary>
        private readonly ReaderWriterLock readerWriterLock;

        /// <summary>
        /// The lock slim.
        /// </summary>
        private readonly ReaderWriterLockSlim readerWriterLockSlim;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="SafeEnumerableDecorator{T}"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        /// <param name="readerWriterLock">
        /// The reader writer lock.
        /// </param>
        public SafeEnumerableDecorator(IEnumerable<T> adaptedObject, ReaderWriterLock readerWriterLock)
            : this(adaptedObject)
        {
            Guard.ArgumentNotNull(readerWriterLock, "readerWriterLock");
            this.readerWriterLock = readerWriterLock;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SafeEnumerableDecorator{T}"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        /// <param name="readerWriterLockSlim">
        /// The reader writer lock slim.
        /// </param>
        public SafeEnumerableDecorator(IEnumerable<T> adaptedObject, ReaderWriterLockSlim readerWriterLockSlim)
            : this(adaptedObject)
        {
            Guard.ArgumentNotNull(readerWriterLockSlim, "readerWriterLockSlim");
            this.readerWriterLockSlim = readerWriterLockSlim;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SafeEnumerableDecorator{T}"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        private SafeEnumerableDecorator(IEnumerable<T> adaptedObject)
        {
            Guard.ArgumentNotNull(adaptedObject, "adaptedObject");
            this.adaptedObject = adaptedObject;
        }

        #endregion Constructors and Finalizers

        #region Public Methods and Operators

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        public IEnumerator<T> GetEnumerator()
        {
            if (this.readerWriterLock != null)
            {
                return new SafeEnumeratorDecorator<T>(this.adaptedObject.GetEnumerator(), this.readerWriterLock);
            }

            return new SafeEnumeratorDecorator<T>(this.adaptedObject.GetEnumerator(), this.readerWriterLockSlim);
        }

        #endregion Public Methods and Operators

        #region Explicit Interface Methods

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion Explicit Interface Methods
    }
}